package com.xbongbong.paas.util;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.RefundEnum;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 用于es中搜索条件对象的封装
 * @author 吴峰
 * @version v1.0
 * @date 2019/4/30 14:26
 * @since v1.0
 */
public class EsConditionUtil {


    public static void isRed(List<ConditionsEntityExt> conditions, Integer isRed) {
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt(ContractEnum.IS_RED.getAttr(), "",
                ContractEnum.IS_RED.getFieldType(), ConditionEnum.EQUAL.getSymbol(), Arrays.asList(isRed));
        conditions.add(conditionsEntityExt);
    }

    public static void allOutBound(List<ConditionsEntityExt> conditions, Integer allOutBound) {
        ConditionsEntityExt allOutBoundConditions = new ConditionsEntityExt(ContractEnum.ALL_OUT_BOUND.getAttr(), "",
                ContractEnum.ALL_OUT_BOUND.getFieldType(), ConditionEnum.EQUAL.getSymbol(), Arrays.asList(allOutBound));
        conditions.add(allOutBoundConditions);
    }

    /**
     * 组装退货退款单查询条件
     * @param conditions
     * @param allInBound
     */
    public static void allInBound(List<ConditionsEntityExt> conditions, Integer allInBound) {
        ConditionsEntityExt allInBoundConditions = new ConditionsEntityExt(RefundEnum.ALL_IN_BOUND.getAttr(), "",
                RefundEnum.ALL_IN_BOUND.getFieldType(), ConditionEnum.EQUAL.getSymbol(), Arrays.asList(allInBound));
        conditions.add(allInBoundConditions);
    }

    /**
     * 拼接搜索条件列表
     * @param conditions 搜索条件的列表
     * @param attr 搜索字段
     * @param fieldType 搜索字段类型
     * @param symbol 搜索字段条件
     * @param value 搜索字段值
     */
    public static void getConditionList(List<ConditionsEntityExt> conditions, String attr, String subAttr, Integer fieldType, String symbol, List<Object> value) {
        ConditionsEntityExt allOutBoundConditions = new ConditionsEntityExt(attr, "", fieldType, symbol, value);
        conditions.add(allOutBoundConditions);
    }

    /**
     * 拼接条件对象
     * @param attr 搜索字段
     * @param subAttr 搜索子表单字段
     * @param fieldType 搜索字段类型
     * @param symbol 搜索字段条件
     * @param value 搜索字段值
     * @return
     */
    public static ConditionsEntityExt getConditionEntity(String attr, String subAttr, Integer fieldType, String symbol, List<Object> value) {
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(attr);
        if (StringUtil.isNotEmpty(subAttr)) {
            conditionsEntityExt.setSubAttr(subAttr);
        }
        conditionsEntityExt.setFieldType(fieldType);
        conditionsEntityExt.setSymbol(symbol);
        if (Objects.nonNull(value)) {
            conditionsEntityExt.setValue(value);
        }
        return conditionsEntityExt;
    }
}
